package com.atguigu.gmall.product.cron;

import com.atguigu.gmall.common.SkuBloomTask;
import com.atguigu.gmall.product.mapper.SkuInfoMapper;
import com.atguigu.gmall.product.service.SkuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2024/9/18 1:44
 */
@Slf4j
@Component
public class SkuIdBloomTask implements SkuBloomTask {
    @Autowired
    SkuInfoMapper skuInfoMapper;
    @Qualifier("skuBloomFilter")
    @Autowired
    RBloomFilter<Object> bloomFilter;

    //周三晚上三点
    @Scheduled(cron ="0 0 3 * * 3")
    public void rebuildBloom(){
        //重建bloom
        log.info("重建skuId布隆过滤器...");
        bloomFilter.delete();
        bloomFilter.tryInit(500000L, 0.00001);
        initSkuIdBloom(bloomFilter);
    }
    @Override
    public void initSkuIdBloom(RBloomFilter<Object> bloomFilter){
        List<Long> ids = skuInfoMapper.getIds();
        log.info("正在初始化skuId布隆过滤器....");
        for (Long id : ids) {
            bloomFilter.add(id);
        }
    }
}
